package com.itheima.xx.reflect_r;

import com.itheima.xx.domain.Student;
import org.junit.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * Description:
 *
 * @author codermao
 * @date 2023/10/23 21:21
 */
public class GetClassDemo {
    @Test
    public void test01() throws ClassNotFoundException {
        Class<Student> clazz = Student.class;
        Class<? extends Student> clazz1 = new Student().getClass();
        Class<?> clazz2 = Class.forName("com.itheima.xx.domain.Student");

        System.out.println(clazz == clazz1);
        System.out.println(clazz1 == clazz2);
    }

    @Test
    public void test02() {
        try{
            Class<Student> clazz = Student.class;
            Constructor<Student> con = clazz.getConstructor(int.class,String.class,int.class);
            System.out.println(con);
            Student msk = con.newInstance(1001,"msk",23);
            System.out.println(msk);
            msk.show();
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void test03(){
        try {
            Class<Student> clazz = Student.class;
            Method method = clazz.getMethod("getDoubleNum", int.class);

            Student instance = clazz.newInstance();

            int result = (int) method.invoke(instance,20);
            System.out.println(result);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }

        //dom4j xpath lombok sl4j druid poj3

    }


    @Test
    public void test05() throws NoSuchMethodException, NoSuchFieldException {
        Class<BookStore> clazz = BookStore.class;

        //
        Constructor<BookStore> constructor = clazz.getConstructor();

        //
        Method method = clazz.getMethod("test");

        //
        Field book = clazz.getDeclaredField("book");

        if (method.isAnnotationPresent(Book.class)){
            Book bookAnno = method.getAnnotation(Book.class);

            String name = bookAnno.name();
            String[] author = bookAnno.author();
            double price = bookAnno.price();

            System.out.println(name + "--" + Arrays.toString(author) + "--" + price);
        }
    }
}
